package ServerToCOImpl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;

import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.w3c.dom.*;

import javax.xml.parsers.*;

import DatabasePO.*;

public class ParseToPO {
	public static ArrayList<ClassPO> parseToClass(File file){
		ArrayList<ClassPO> classes=new ArrayList<ClassPO>();
		 try {
	            // 创建解析器工厂
	            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	            // 获得解析器
	            DocumentBuilder db = dbf.newDocumentBuilder();
	            // 对XMl文档进行解析，得到Document
	            Document doc = db.parse(file.getAbsolutePath());
	            // 得到所有列表信息
	            NodeList classList = doc.getElementsByTagName("classA");
	            // 循环class信息
	            if(classList !=null && classList.getLength()>0){
	                
	                for (int i = 0; i < classList.getLength(); i++) {
	                    Node oneClass = classList.item(i);
	                    NodeList infoList = oneClass.getChildNodes();
	                    ClassPO OneClass=new ClassPO();
	                    for (int j = 0; j < infoList.getLength(); j++) {
	                        Node typeElement = (Node) infoList.item(j);
	                        if(typeElement instanceof Element) {
	                              Element element = (Element) typeElement;
	                              Text content = (Text)element.getFirstChild();
	                              
	                              if(element.getNodeName().equals("课程编号"))
	                            	  OneClass.setNumber(content.getData());
	                              else if(element.getNodeName().equals("课程名称"))
	                            	  OneClass.setName(content.getData());
	                              else if(element.getNodeName().equals("学分"))
	                            	  OneClass.setPoint(content.getData());
	                              else if(element.getNodeName().equals("授课老师"))
	                            	  OneClass.setTeacher(content.getData());
	                              else if(element.getNodeName().equals("授课地点"))
	                            	  OneClass.setPlace(content.getData());
	                              else if(element.getNodeName().equals("共享"))
	                            	  OneClass.setShare(content.getData());
	                        }
	                        
	                    }
	                    classes.add(OneClass);
	                }
	            }
	    
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		return classes;
	}
	
	public static ArrayList<ClassChoicePO> parseToChoice(File file){
		ArrayList<ClassChoicePO> classChoices=new ArrayList<ClassChoicePO>();
		 try {
	            // 创建解析器工厂
	            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	            // 获得解析器
	            DocumentBuilder db = dbf.newDocumentBuilder();
	            // 对XMl文档进行解析，得到Document
	            Document doc = db.parse(file.getAbsolutePath());
	            // 得到所有列表信息
	            NodeList choiceList = doc.getElementsByTagName("choiceA");
	            // 循环choice信息
	            if(choiceList !=null && choiceList.getLength()>0){
	                
	                for (int i = 0; i < choiceList.getLength(); i++) {
	                    Node oneClass = choiceList.item(i);
	                    NodeList infoList = oneClass.getChildNodes();
	                    ClassChoicePO OneClassChoice=new ClassChoicePO();
	                    for (int j = 0; j < infoList.getLength(); j++) {
	                        Node typeElement = (Node) infoList.item(j);
	                        if(typeElement instanceof Element) {
	                              Element element = (Element) typeElement;
	                              Text content = (Text)element.getFirstChild();
	                              
	                              if(element.getNodeName().equals("课程编号"))
	                            	  OneClassChoice.setClassNumber(content.getData());
	                              else if(element.getNodeName().equals("学生编号"))
	                            	  OneClassChoice.setStudentNumber(content.getData());
	                              else if(element.getNodeName().equals("成绩"))
	                            	  OneClassChoice.setGrade(Integer.parseInt(content.getData()));
	                        }
	                        
	                    }
	                    classChoices.add(OneClassChoice);
	                }
	            }
	    
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		return classChoices;
	}
}
